package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //根据分类不同的ID显示菜品的信息
    @GetMapping("/dish/list")
    public ResultInfo findByCategoryId(Long categoryId,Integer status){
        //status是状态，目前是1

        //设置一个key
        String key = "dish"+categoryId;
        //从Redis中查询，如果有直接从Redis中获取，如果没有，从mysql中获取

        List<Dish> dishList = (List<Dish>) redisTemplate.opsForValue().get(key);
        if (CollectionUtil.isNotEmpty(dishList)){
            System.out.println("这是从Redis中查询的");
        }else {
            System.out.println("这是从mysql中查询的");
            //Redis中查不到，从MySQL中获取
            dishList= dishService.findByCategoryId(categoryId);
            redisTemplate.opsForValue().set(key,dishList);

        }
        return ResultInfo.success(dishList);
    }

}
